
package stcu;

import stcu.robjects.*;
import com.nvlled.route.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class Server {

    private static final String ACCOUNTS_FILENAME = "accounts";
    
    private ProbeMonitor probeMonitor;
    private ObjectRouter router;
    private ServerAuth auth;
    private Group defaultGroup;

    String tempServerName = "dutch mill yoghurt";

    public Server(ObjectRouter router) {
        this.router = router;
        auth = new ServerAuth(router);
        defaultGroup = new DefaultGroup(router, auth);
        probeMonitor = new ProbeMonitor(tempServerName, router);
        GroupDemux demux = new GroupDemux(router);

        demux.register(defaultGroup);
        for (String user: readAccounts()) {
            auth.registerUser(user);
        }
    }

    private List<String> readAccounts() {
        List<String> accounts = new ArrayList<String>();
        Scanner in = null;
        try {
            in = new Scanner(new File(ACCOUNTS_FILENAME));
        } catch (FileNotFoundException e) {
            javax.swing.JOptionPane.showMessageDialog(null, "No accounts file found.");
        }

        while (in.hasNextLine()) {
            try {
                String name = in.nextLine().trim();
                if ( ! name.isEmpty()) {
                    accounts.add(name);
                }
            } catch (NoSuchElementException e) { break; }
        }
        return accounts;
    }



    public static void main(String args[]) throws Exception { 
        final ObjectRouter router = new ObjectRouter();
        Server server = new Server(router);

        new Thread(router).start();
        System.out.println("Server started.");
        new stcu.forms.ServerForm().setVisible(true);
        router.bindBroadcast();
    }
}

